<template>
  <div class="user-order">
    <Header/>
    <div class="my-tab">
      <ul>
        <li>我的账户</li>
        <li class="my-active">我的订单</li>
        <li>个人资料</li>
      </ul>
    </div>
    <div class="main">
      <div class="user-data">
        <ul class="nav">
          <li class="order-info1">订单/课程详情</li>
          <li class="course-expire1">有效期</li>
          <li class="course-price1">课程价格</li>
          <li class="real-price1">实付金额</li>
          <li class="order-status1">交易状态</li>
          <li class="order-do1">交易操作</li>
        </ul>
        <div class="my-order-item" v-for="(order,index) in order_list">
          <div class="user-data-header">
            <span class="order-time">{{order.pay_time}}</span>
            <span class="order-num">订单号：
                        <span class="my-older-number">{{order.order_number}}</span>
                    </span>
          </div>
          <ul class="nav user-data-list" v-for="(course,i) in order.course_list">
            <li class="order-info">
              <img :src="course.course_img" alt="">
              <div class="order-info-title">
                <p class="course-title">{{course.course_name}}</p>
                <p class="price-service">{{course.discount_name}}</p>
              </div>
            </li>
            <li class="course-expire">{{course.expire_text}}</li>
            <li class="course-price">{{course.origin_price}}</li>
            <li class="real-price">{{course.real_price}}</li>
            <li class="order-status" v-if="order.order_status===0">未支付</li>
            <li class="order-status" v-else-if="order.order_status===1">已支付</li>
            <li class="order-status" v-else-if="order.order_status===2">已取消</li>
            <li class="order-status" v-else-if="order.order_status===3">超时取消</li>

            <li class="order-do">
              <span class="btn btn2" v-if="order.order_status===0" @click="gopay(order.order_number)">去支付</span>
              <span class="btn btn2" v-else-if="order.order_status===1">
                <router-link to="/course/player">去学习</router-link></span>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <Footer/>
  </div>
</template>

<script>
    import Header from "./common/Header"
    import Footer from "./common/Footer"

    export default {
        name: "UserOrder",
        data() {
            return {
                order_list: [],
            };
        },

        created() {
            this.get_user_order();
        },

        methods: {
            // 点击去支付
            gopay(order_number) {
                this.$axios.get(`${this.$settings.Host}/payments/alipay/`, {
                    params: {
                        order_number: order_number,
                    }
                })
                    .then((response) => {
                        // console.log(response);
                        location.href = response.data;
                    })
                    .catch((error) => {
                        console.log(error.response.data);
                    })
            },

            // 检查当前访问者是否登录！
            check_user_login() {
                let token = sessionStorage.user_token || localStorage.user_token;
                if (!token) {
                    this.$confirm('您尚未登录，请先登录官网！', '路飞学城官网温馨提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.$router.push('/login/');
                    });
                    return false;
                }
                return token
            },

            // 获取当前登录用户的所有订单
            get_user_order() {
                let token = this.check_user_login();
                this.$axios.get(`${this.$settings.Host}/order/list/`, {
                    headers: {
                        "Authorization": "jwt " + token,
                    }
                })
                    .then((res) => {
                        this.order_list = res.data;
                    })
                    .catch((error) => {
                        console.log(error.response);
                    })
            },
        },
        components: {
            Header,
            Footer,
        }
    }
</script>

<style scoped>

  .my-tab {
    width: 100%;
    height: 50px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .my-tab ul {
    width: 1200px;
    line-height: 50px;
    height: auto;
    display: flex;
    flex-wrap: wrap;
  }

  .my-tab ul li {
    height: 35px;
    margin-right: 30px;
    padding-bottom: 3px;
    font-size: 15px;
    font-weight: 400;
    color: #5e5e5e;
  }

  .my-active {
    font-weight: 500;
    color: #333 !important;
    box-shadow: 0 2px 0 0 #f5a623;
  }

  .main {
    background: #f6f6f6;
    overflow: hidden;
  }

  .user-data {
    width: 1200px;
    height: auto;
    margin: 0 auto;
    padding-top: 30px;
    border-top: 1px solid #e8e8e8;
    margin-bottom: 63px;
  }

  .nav {
    width: 100%;
    height: 60px;
    background: #FAFAFA;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-align: center;
    align-items: center;
  }

  .nav li {
    height: 60px;
    line-height: 60px;
    list-style: none;
    font-size: 15px;
    font-weight: 400;
    color: #333;
  }

  .nav .order-info1 {
    width: 450px;
    margin-left: 60px;
  }

  .nav .course-expire1 {
    width: 150px;
  }

  .nav .course-price1 {
    width: 150px;
  }

  .nav .real-price1 {
    width: 150px;
  }

  .nav .order-status1 {
    width: 130px;
  }

  .nav .order-do1 {
    width: 110px;
  }

  .user-data-header {
    display: flex;
    height: 44px;
    color: #4a4a4a;
    font-size: 14px;
    background: #fcf7ef;
    -ms-flex-align: center;
    align-items: center;
  }

  .user-data-list {
    background: #FAFAFA;
    height: 100%;
    display: flex;
  }

  .user-data-list li {
    height: 60px;
    line-height: 60px;
    list-style: none;
    font-size: 14px;
    font-weight: 400;
    color: #333;
    border-bottom: 1px solid #e9e9e9;
  }

  .order-time {
    font-size: 13px;
    display: inline-block;
    margin-left: 30px;
  }

  .order-num {
    font-size: 13px;
    display: inline-block;
    margin-left: 20px;
  }

  .user-data-list .order-info {
    width: 437px;
    display: flex;
    align-items: center;
    margin-left: 30px;
  }

  .user-data-list .order-info img {
    max-width: 100px;
    max-height: 75px;
    margin-right: 22px;
  }

  .course-title {
    width: 203px;
    font-size: 13px;
    color: #333;
    line-height: 4px;
    margin-top: -3px;
  }

  .order-info-title .price-service {
    line-height: 18px;
  }

  .price-service {
    font-size: 13px;
    color: #fa6240;
    padding: 0 5px;
    border: 1px solid #fa6240;
    border-radius: 4px;
    margin-top: 0px;
    position: absolute;
  }

  .order-info-title {
    margin-top: -10px;
  }

  .user-data-list .course-expire {
    font-size: 13px;
    color: #ff5502;
    width: 135px;
    text-align: center;
  }

  .user-data-list .course-price {
    width: 170px;
    text-align: center;
  }

  .user-data-list .real-price {
    width: 135px;
    text-align: center;
  }

  .user-data-list .order-status {
    width: 165px;
    text-align: center;
  }

  .btn {
    width: 100px;
    height: 32px;
    font-size: 14px;
    color: #fff;
    background: #ffc210;
    border-radius: 4px;
    border: none;
    outline: none;
    transition: all .25s ease;
    display: inline-block;
    line-height: 32px;
    text-align: center;
    cursor: pointer;
  }
</style>
